from building import *
import os
import zipfile

user_libraries_root_path = GetCurrentDir()
inc = [user_libraries_root_path]
src = Glob('*.c') + Glob('*.cpp')

# scripts_path = os.path.join(user_libraries_root_path, '..', '..', 'scripts')
# sys.path.append(scripts_path)

# run ALM script
# alm_path = os.path.join(scripts_path, 'ALM')
# sys.path.append(alm_path)
# from ALM_CMD import ALM_Init
# ALM_Init(alm_path)

def rtduino_scons_add_library(library_root_path):
    global src, inc

    library_root_path = os.path.abspath(library_root_path)

    # Arduino libraries must have the 'library.properties' file.
    # If this folder contains SConscript, just follow the SConscript rule rather than the Arduino rule.
    if os.path.isdir(library_root_path) == False or \
       os.path.exists(os.path.join(library_root_path, 'library.properties')) == False or \
       os.path.exists(os.path.join(library_root_path, 'SConscript')) == True:
        return

    library_src_path = os.path.join(library_root_path, 'src') # Arduino library source code folder
    library_utility_path = os.path.join(library_root_path, 'utility') # Arduino library utility folder

    # add all .c/cpp files and header files' path in root folder (not include sub-folders)
    inc = inc + [library_root_path] # add root folder header files' path
    src = src + Glob(os.path.join(library_root_path, '*.c')) # add all .c file
    src = src + Glob(os.path.join(library_root_path, '*.cpp')) # add all .cpp file

    # add root, src and untility folder header files' path (not include sub-folders)
    if os.path.exists(library_src_path):
        inc = inc + [library_src_path]
        src = src + Glob(os.path.join(library_src_path, '*.c'))
        src = src + Glob(os.path.join(library_src_path, '*.cpp'))
    if os.path.exists(library_utility_path):
        inc = inc + [library_utility_path]
        src = src + Glob(os.path.join(library_utility_path, '*.c'))
        src = src + Glob(os.path.join(library_utility_path, '*.cpp'))

    # find the src folder's sub-folders and sub-files recursively
    for root, dirs, files in os.walk(library_src_path):
        for dir in dirs: # add all sub-folders' header files' path
            _sub_path = os.path.join(root,dir) # sub-folder path
            # add all .c/cpp file and .h/hpp file
            src = src + Glob(os.path.join(_sub_path, '*.c'))
            src = src + Glob(os.path.join(_sub_path, '*.cpp'))

# Unzip all zip files in the root of 'libraries' folder
for file in os.listdir(user_libraries_root_path):
    if os.path.isfile(file) == True:
        splitext = os.path.splitext(file)
        if splitext[1] == '.zip':
            # unzip the file
            zFile = zipfile.ZipFile(os.path.join(user_libraries_root_path, file), 'r')
            for fileM in zFile.namelist():
                zFile.extract(fileM, '.')
            zFile.close()
            os.remove(file) # delete the zip file

# Add all user libraries at the root of 'libraries' folder into the group
for dir in os.listdir(user_libraries_root_path):
    rtduino_scons_add_library(os.path.join(user_libraries_root_path, dir))

# Add all Arduino libraries at the root of 'package' folder into the group
rtthread_packages_root_path = os.path.join(user_libraries_root_path, '..', '..', '..')
for dir in os.listdir(rtthread_packages_root_path):
    rtduino_scons_add_library(os.path.join(rtthread_packages_root_path, dir))

group = DefineGroup('RTduino-libraries', src, depend = ['PKG_USING_RTDUINO'], CPPPATH = inc)

Return('group')
